package toubiao.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.Field;

import toubiao.domain.vo.assist.Json;
import toubiao.domain.vo.assist.PageHelper;
import toubiao.domain.vo.business.ContractGrid;
import toubiao.domain.vo.business.ProjectDetail;
import toubiao.domain.vo.easyUi.DataGrid;
import toubiao.exception.CustomException;
import toubiao.service.impl.ProjectServiceImpl;
import toubiao.utils.ProUtilFileSuffix;
import toubiao.utils.filter.FilterCondition;
import toubiao.utils.filter.FilterHqlFactory;
import toubiao.utils.filter.OrderCondition;
import toubiao.utils.filter.OrderHqlFactory;
import utils.UtilController;
import utils.UtilDate;
import utils.UtilJackson;
import utils.UtilString;
import utils.easyui.domain.EzDatagrid;

@Controller
@RequestMapping("/project")
public class ProjectController {
	private Logger logger=LoggerFactory.getLogger(ProjectController.class);
	
	@Autowired
	private ProjectServiceImpl projectService;
	
	@RequestMapping("/grid.jspPage")
	public String JspPageGetProjectGrid(){
		return "/module/project/projectGrid";
	}
	
	@RequestMapping("/detail.jspPage")
	public String JspPageGetProjectDetail(){
		return "/module/project/projectDetail4";
	}

	@RequestMapping("/detail/{projectId}/prop.json")
	@ResponseBody
	public Json JsonGetProjectDetail(@PathVariable("projectId") String projectId){
		return projectService.getProjectDetail(projectId);
	}
	
	@RequestMapping("/detail/update/json")
	@ResponseBody
	public Json JsonUpdateProjectDetail(ProjectDetail projectDetail){
		//UtilJackson.getJson(projectDetail);
		return projectService.updateProjectDetail(projectDetail);
	}
	

	@RequestMapping("/detail/{projectId}/contractDatagrid.json")
	@ResponseBody
	public EzDatagrid<ContractGrid> datagridGetContractsOfProject(@PathVariable("projectId")String projectId) throws InstantiationException, IllegalAccessException, CustomException{
		return projectService.getContractDataGridByProjectId(projectId);
	}
	
	@RequestMapping("/detail/updateContractLink.json")
	@ResponseBody
	public Json jsonUpdateLinkWithContract(String projectId,String contractIds){
		return projectService.updateLinkWithContract(projectId,contractIds);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 通过分页形式，获取项目信息，其中包换过滤信息和排序信息:conditionsJson;orderConditionJson
	 * 
	 * @param project
	 * @param PageHelper
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/gridData.json")
	@ResponseBody
	public DataGrid jsonDataGridByHqlCondition( PageHelper ph,HttpServletRequest request)  {
		String filterConditionsJson=request.getParameter("conditionsJson");
		String orderConditionsJson=request.getParameter("orderConditionJson");
		List<FilterCondition> filterConditionList=FilterHqlFactory.json2FilterConditionList(filterConditionsJson);
		List<OrderCondition> orderConditionList=OrderHqlFactory.json2OrderConditionList(orderConditionsJson);
		
		
		//logger.info(conditionList.size()+"");
		for(FilterCondition filterCondition:filterConditionList){
			logger.info(filterCondition.toString());
		}
		
		DataGrid dataGrid=projectService.getDataGridByConditions(filterConditionList, orderConditionList, ph);
		return dataGrid;
	}
	
	@RequestMapping("/downloadAll")
	public void fileDownloadAll(HttpServletRequest request, HttpServletResponse response) throws CustomException, IOException{
		String fileType=request.getParameter("fileType");
		String fileAbsPath=projectService.downloadFileAll(fileType);
		String suffix=ProUtilFileSuffix.getFileSuffix(fileType);
        String dateString=UtilDate.getFormateDate(new Date(), "yyMMdd");
        String savedName="所有项目_"+dateString+suffix;
        UtilController.downloadFile(fileAbsPath, savedName, request, response);
	}

	//下载Excel checked
	@RequestMapping("/downloadChecked")
	public void fileDownloadChecked(HttpServletRequest request, HttpServletResponse response,String ids) throws Exception{
		if(ids==null || ids.trim().equals("")){
			throw new CustomException("ids 为空.");
		}
		String fileType=request.getParameter("fileType");
		String suffix=ProUtilFileSuffix.getFileSuffix(fileType);
		String fileAbsPath=projectService.downloadFileCheckedByIds(fileType, ids);
		
        String dateString=UtilDate.getFormateDate(new Date(), "yyMMdd");
        String savedName="勾选项目_"+dateString+suffix;
        UtilController.downloadFile(fileAbsPath, savedName, request, response);
	}
	
	//下载Excel filtered
	@RequestMapping("/downloadFiltered")
	public void fileDownloadFiltered(HttpServletRequest request, HttpServletResponse response) throws CustomException, IOException{
		String filterConditionsJson=request.getParameter("conditionsJson");
		String orderConditionsJson=request.getParameter("orderConditionJson");
		String fileType=request.getParameter("fileType");
		String suffix=ProUtilFileSuffix.getFileSuffix(fileType);
		List<FilterCondition> filterConditionList=FilterHqlFactory.json2FilterConditionList(filterConditionsJson);
		List<OrderCondition> orderConditionList=OrderHqlFactory.json2OrderConditionList(orderConditionsJson);
		
		String fileAbsPath=projectService.downloadFileByConditions(fileType, filterConditionList, orderConditionList);
		String dateString=UtilDate.getFormateDate(new Date(), "yyMMdd");
        String savedName="筛选项目_"+dateString+suffix;
        UtilController.downloadFile(fileAbsPath, savedName, request, response);
	}
}
